package com.xjwlmq.exam20241012;

import com.alibaba.fastjson.JSON;
import com.xjwlmq.exam20241011.DictType;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;

/**
 * @ClassName WorkerListServlet
 * @Description TODO:
 * @Author 12994
 * @Date 2024/10/12 20:49
 * @Version 1.0
 **/
@WebServlet("/getWorkerList")
public class WorkerListServlet extends HttpServlet {
    WorkerDaoImpl workerDao = new WorkerDaoImpl();

    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setCharacterEncoding("UTF-8");
        resp.setContentType("text/html:charset=UTF-8");

        if (req.getParameter("worker_id") != null) {
            ArrayList<Worker> workers = workerDao.getWorkers(Integer.parseInt(req.getParameter("worker_id")));
            resp.getWriter().write(JSON.toJSONString(workers));
        } else {
            ArrayList<Worker> workers = workerDao.getWorkers();
            resp.getWriter().write(JSON.toJSONString(workers));
        }
    }
}
